{#

This file is part of the Sonata package.

(c) Thomas Rabaix <thomas.rabaix@sonata-project.org>

For the full copyright and license information, please view the LICENSE
file that was distributed with this source code.

#}

{% extends base_template %}

{% block actions %}
{% spaceless %}
    {% if admin.hasRoute('create') and admin.isGranted('CREATE')%}
        <li>{% include 'SonataAdminBundle:Core:create_button.html.twig' %}</li>
    {% endif %}
{% endspaceless %}
{% endblock %}

{% block tab_menu %}{{ knp_menu_render(admin.sidemenu(action), {'currentClass' : 'active', 'template': admin_pool.getTemplate('tab_menu_template')}, 'twig') }}{% endblock %}


{% block list_table %}
    <div class="box box-primary">
        <div class="box-body table-responsive no-padding">
            {{ sonata_block_render_event('sonata.admin.list.table.top', { 'admin': admin }) }}

            {% block list_header %}{% endblock %}

            {% set batchactions = admin.batchactions %}
            {% if admin.datagrid.results|length > 0 %}
                {% if admin.hasRoute('batch') %}
                <form action="{{ admin.generateUrl('batch', {'filter': admin.filterParameters}) }}" method="POST" >
                    <input type="hidden" name="_sonata_csrf_token" value="{{ csrf_token }}">
                {% endif %}
                    <table class="table table-bordered table-striped">
                        {% block table_header %}
                            <thead>
                                <tr class="sonata-ba-list-field-header">
                                    {% for field_description in admin.list.elements %}
                                        {% if admin.hasRoute('batch') and field_description.getOption('code') == '_batch' and batchactions|length > 0 %}
                                            <th class="sonata-ba-list-field-header sonata-ba-list-field-header-batch">
                                              <input type="checkbox" id="list_batch_checkbox">
                                            </th>
                                        {% elseif field_description.getOption('code') == '_select' %}
                                            <th class="sonata-ba-list-field-header sonata-ba-list-field-header-select"></th>
                                        {% elseif field_description.name == '_action' and app.request.isXmlHttpRequest %}
                                            {# Action buttons disabled in ajax view! #}
                                        {% elseif field_description.getOption('ajax_hidden') == true and app.request.isXmlHttpRequest %}
                                            {# Disable fields with 'ajax_hidden' option set to true #}
                                        {% else %}
                                            {% set sortable = false %}
                                            {% if field_description.options.sortable is defined and field_description.options.sortable %}
                                                {% set sortable             = true %}
                                                {% set sort_parameters      = admin.modelmanager.sortparameters(field_description, admin.datagrid) %}
                                                {% set current              = admin.datagrid.values._sort_by == field_description or admin.datagrid.values._sort_by.fieldName == sort_parameters.filter._sort_by %}
                                                {% set sort_active_class    = current ? 'sonata-ba-list-field-order-active' : '' %}
                                                {% set sort_by              = current ? admin.datagrid.values._sort_order : field_description.options._sort_order %}
                                            {% endif %}

                                            {% spaceless %}
                                                <th class="sonata-ba-list-field-header-{{ field_description.type}} {% if sortable %} sonata-ba-list-field-header-order-{{ sort_by|lower }} {{ sort_active_class }}{% endif %}">
                                                    {% if sortable %}<a href="{{ admin.generateUrl('list', sort_parameters) }}">{% endif %}
                                                    {{ admin.trans(field_description.label, {}, field_description.translationDomain) }}
                                                    {% if sortable %}</a>{% endif %}
                                                </th>
                                            {% endspaceless %}
                                        {% endif %}
                                    {% endfor %}
                                </tr>
                            </thead>
                        {% endblock %}

                        {% block table_body %}
                            <tbody>
                                {% for object in admin.datagrid.results %}
                                    <tr>
                                        {% include admin.getTemplate('inner_list_row') %}
                                    </tr>
                                {% endfor %}
                            </tbody>
                        {% endblock %}

                        {% block table_footer %}
                            <tfoot>
                                <tr>
                                    <th colspan="{{ admin.list.elements|length - (app.request.isXmlHttpRequest ? (admin.list.has('_action') + admin.list.has('batch')) : 0) }}">
                                        <div class="form-inline">
                                            {% if not app.request.isXmlHttpRequest %}
                                                {% if admin.hasRoute('batch') and batchactions|length > 0  %}
                                                    {% block batch %}
                                                        <script>
                                                            {% block batch_javascript %}
                                                                jQuery(document).ready(function ($) {
                                                                    $('#list_batch_checkbox').on('ifChanged', function () {
                                                                        $(this)
                                                                            .closest('table')
                                                                            .find('td.sonata-ba-list-field-batch input[type="checkbox"]')
                                                                            .iCheck($(this).is(':checked') ? 'check' : 'uncheck')
                                                                        ;
                                                                    });

                                                                    $('td.sonata-ba-list-field-batch input[type="checkbox"]')
                                                                        .on('ifChanged', function () {
                                                                            $(this)
                                                                                .closest('tr')
                                                                                .toggleClass('sonata-ba-list-row-selected', $(this).is(':checked'))
                                                                            ;
                                                                        })
                                                                        .trigger('ifChanged')
                                                                    ;
                                                                });
                                                            {% endblock %}
                                                        </script>

                                                        {% block batch_actions %}
                                                            <label class="checkbox" for="{{ admin.uniqid }}_all_elements">
                                                                <input type="checkbox" name="all_elements" id="{{ admin.uniqid }}_all_elements">
                                                                {{ 'all_elements'|trans({}, 'SonataAdminBundle') }}
                                                                 ({{ admin.datagrid.pager.nbresults }})
                                                            </label>

                                                            <select name="action" style="width: auto; height: auto" class="form-control">
                                                                {% for action, options in batchactions %}
                                                                    <option value="{{ action }}">{{ options.label }}</option>
                                                                {% endfor %}
                                                            </select>
                                                        {% endblock %}

                                                        <input type="submit" class="btn btn-small btn-primary" value="{{ 'btn_batch'|trans({}, 'SonataAdminBundle') }}">
                                                    {% endblock %}
                                                {% endif %}

                                                <div class="pull-right">
                                                    {% if admin.hasRoute('export') and admin.isGranted("EXPORT") and admin.getExportFormats()|length %}
                                                        <div class="btn-group">
                                                            <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
                                                                <i class="glyphicon glyphicon-export"></i>
                                                                {{ "label_export_download"|trans({}, "SonataAdminBundle") }}
                                                                <span class="caret"></span>
                                                            </button>
                                                            <ul class="dropdown-menu">
                                                                {% for format in admin.getExportFormats() %}
                                                                    <li>
                                                                        <a href="{{ admin.generateUrl('export', admin.modelmanager.paginationparameters(admin.datagrid, 0) + {'format' : format}) }}">
                                                                            <i class="glyphicon glyphicon-download"></i>
                                                                            {{ format|upper }}
                                                                        </a>
                                                                    <li>
                                                                {% endfor %}
                                                            </ul>
                                                        </div>

                                                        &nbsp;-&nbsp;
                                                    {% endif %}

                                                    {% block pager_results %}
                                                        {% include admin.getTemplate('pager_results') %}
                                                    {% endblock %}
                                                </div>
                                            {% endif %}
                                        </div>
                                    </th>
                                </tr>

                                {% block pager_links %}
                                    {% if admin.datagrid.pager.haveToPaginate() %}
                                        {% include admin.getTemplate('pager_links') %}
                                    {% endif %}
                                {% endblock %}

                            </tfoot>
                        {% endblock %}
                    </table>
                {% if admin.hasRoute('batch') %}
                </form>
                {% endif %}
            {% else %}
                <div class="callout callout-info">
                    {{ 'no_result'|trans({}, 'SonataAdminBundle') }}
                </div>
            {% endif %}

            {% block list_footer %}{% endblock %}

            {{ sonata_block_render_event('sonata.admin.list.table.bottom', { 'admin': admin }) }}


        </div>
    </div>
{% endblock %}

{% block list_filters %}
    {% if admin.datagrid.filters %}
        {% form_theme form admin.getTemplate('filter') %}
        <div class="box box-primary">
            <div class="box-header">
                <h4 class="box-title filter_legend {{ admin.datagrid.hasActiveFilters ? 'active' : 'inactive' }}">{{ 'label_filters'|trans({}, 'SonataAdminBundle') }}</h4>
            </div>

            <div class="box-body">
                <form class="sonata-filter-form {{ admin.isChild and 1 == admin.datagrid.filters|length ? 'hide' : '' }}" action="{{ admin.generateUrl('list') }}" method="GET" role="form">
                    {{ form_errors(form) }}
                        <div class="filter_container {{ admin.datagrid.hasActiveFilters ? 'active' : 'inactive' }}">
                            {% for filter in admin.datagrid.filters %}
                                <div class="form-group">
                                    {% if filter.label is not same as(false) %}
                                    <label for="{{ form.children[filter.formName].children['value'].vars.id }}">{{ admin.trans(filter.label, {}, filter.translationDomain) }}</label>
                                    {% endif %}
                                    {% set attr = form.children[filter.formName].children['type'].vars.attr|default({}) %}
                                    {% set attr = attr|merge({'class': (attr.class|default('') ~ ' sonata-filter-option')|trim}) %}

                                    <div>
                                        {{ form_widget(form.children[filter.formName].children['type'], {'attr':  attr}) }}
                                    </div>

                                    <div>
                                        {{ form_widget(form.children[filter.formName].children['value']) }}
                                    </div>
                                </div>
                            {% endfor %}

                            <input type="hidden" name="filter[_page]" id="filter__page" value="1">

                            {% set foo = form.children['_page'].setRendered() %}
                            {{ form_rest(form) }}

                            <button type="submit" class="btn btn-primary"><i class="fa fa-filter"></i> {{ 'btn_filter'|trans({}, 'SonataAdminBundle') }}</button>

                            <a class="btn btn-default" href="{{ admin.generateUrl('list', {filters: 'reset'}) }}">{{ 'link_reset_filter'|trans({}, 'SonataAdminBundle') }}</a>
                        </div>

                        {% for paramKey, paramValue in admin.persistentParameters %}
                            <input type="hidden" name="{{ paramKey }}" value="{{ paramValue }}">
                        {% endfor %}
                </form>
            </div>
        </div>

    {% endif %}
{% endblock %}
